<?php

namespace app\admin\controller;

use think\facade\Session;
use app\admin\controller\Base;
use app\common\model\Category as CategoryModel;

class Category extends Base
{
    public function initialize()
    {
        parent::initialize();
        $this->Model = new CategoryModel();
        $parent_id = $this->request->param('parent_id');
        $this->parent_id = intval($parent_id);
        $this->assign('parent_id', $this->parent_id);
    }

    public function index(){

      $parents = $this->Model->childrenSelect(['parent_id' => 0, 'order_type' => 'id_asc']);
      if(!empty($parents)){
        // 只能查看二级分类，不能查看一级分类
        if(empty($this->parent_id)){
            $this->redirect(url('index', ['parent_id' => $parents[0]['id']]));
        }
        $list = $this->Model->childrenSelect(['parent_id' => $this->parent_id, 'select_with' => 'member']);
      }else{
        $list = [];
      }

      $this->assign('parents', $parents);
      $this->assign('list', $list);
      return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if($this->request->isPost()){
            $data = $this->getPostData();
            $res = $this->Model->createOrUpdate($data);
            if($res["status"]){
                Session::flash('success', $res['msg']);
                $this->success($res["msg"], url('index', ['parent_id' => $this->parent_id]));
            }else{
                Session::flash('danger', $res['msg']);
                $this->error($res["msg"]);
            }
        }else{
            $category = ["status" => 1, "pc_cover_id" => 0, "sort" => 0];
            $this->assign("category", $category);
            return $this->fetch("form");
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id = 0)
    {
        $id = intval($id);
        $category = $this->Model->where("id", $id)->where("parent_id", $this->parent_id)->find();
        if(empty($category)){
            Session::flash('danger', '未找到编辑记录');
            $this->redirect('index');
        }

        if($this->request->isPost()){
          $data = $this->getPostData();
          $res = $this->Model->createOrUpdate($data);
          if($res["status"]){
            Session::flash('success', $res['msg']);
            $this->success($res["msg"], url('index', ['parent_id' => $this->parent_id]));
          }else{
            Session::flash('danger', $res['msg']);
            $this->error($res["msg"]);
          }
        }else{
          $this->assign("category", $category);
          return $this->fetch("form");
        }
    }

    protected function index_default_param(){
        return ['parent_id' => $this->parent_id];
    }

    private function getPostData(){
        $fields = [
            ['name' => 'id', 'type' => 'integer', 'is_null' => true],
            // 使用switch开关，所以默认值必须是false，因为switch为关闭状态时返回的值里没有该字段
            // ['name' => 'status', 'type' => 'boolean', 'default' => false],
            ['name' => 'title', 'type' => 'string', 'default' => ''],
            ['name' => 'sort', 'type' => 'integer', 'default' => 0],
        ];

        $default = [
            'parent_id' => $this->parent_id,
            'pc_banner_id' => 0,
            'wap_banner_id' => 0,
            'status' => true,
        ];

        return $this->parsePostParam($fields, $default);
    }
}
